﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cricket.LinqModel;
using Cricket.LinqData;
using System.Web.Services;

public partial class SessionResult : System.Web.UI.Page
{
    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    MatchSchedule matchSchedule;
    Result result;
    MatchSessionResult matchSessionresult;
    LoggedInUser loggedInUser;


    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            HttpCookie cookie = Request.Cookies["Admin"];
            if (cookie == null)
                Response.Redirect("Login.aspx");
            else
                bindMatch();
        }
    }

    private void bindMatch()
    {
        matchSchedule = new MatchSchedule(_dataObject);
        List<GetMatchesForMatchResultResult> lstResult = new List<GetMatchesForMatchResultResult>();
        lstResult = matchSchedule.GetMatchesForMatchResult();
        if (lstResult.Count > 0)
        {
            drpMatch.DataSource = lstResult;
            drpMatch.DataTextField = "Teams";
            drpMatch.DataValueField = "Id";
            drpMatch.DataBind();
            drpMatch.Items.Insert(0, new ListItem("Select Match", "0"));
        }
    }
    protected void drpMatch_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindOvers();        
    }

    private void bindOvers()
    {
        matchSchedule = new MatchSchedule(_dataObject);
        List<GetTeamsByMatchResult> lstResult = new List<GetTeamsByMatchResult>();
        lstResult = matchSchedule.GetTeamsByMatch(Convert.ToInt32(drpMatch.SelectedValue));
        if (lstResult != null && lstResult.Count > 0)
        {
            Team1Name.InnerHtml = lstResult[0].TeamName;
            hdnTeam1Id.Value = lstResult[0].Id.ToString();
            result = new Result(_dataObject);
            List<GetOversByTeamIdAndMatchIdResult> lst = new List<GetOversByTeamIdAndMatchIdResult>();
            lst = result.GetOversByTeamAndMatch(Convert.ToInt32(lstResult[0].Id), Convert.ToInt32(drpMatch.SelectedValue));
            if (lst.Count > 0)
            {
                rptTeam1.DataSource = lst;
                rptTeam1.DataBind();
            }
            else
            {
                rptTeam1.DataSource = null;
                rptTeam1.DataBind();
            }
            Team2Name.InnerHtml = lstResult[1].TeamName;
            hdnTeam2Id.Value = lstResult[1].Id.ToString();
            result = new Result(_dataObject);
            List<GetOversByTeamIdAndMatchIdResult> lst1 = new List<GetOversByTeamIdAndMatchIdResult>();
            lst1 = result.GetOversByTeamAndMatch(Convert.ToInt32(lstResult[1].Id), Convert.ToInt32(drpMatch.SelectedValue));
            if (lst1.Count > 0)
            {
                rptTeam2.DataSource = lst1;
                rptTeam2.DataBind();
            }
            else
            {
                rptTeam2.DataSource = null;
                rptTeam2.DataBind();
            }

        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
       
        HttpCookie cookie = Request.Cookies["Admin"];
        matchSessionresult= new MatchSessionResult(_dataObject);
        matchSessionresult.MatchScheduleId = Convert.ToInt32(drpMatch.SelectedValue);

        int Over = 0;

        #region Team1
        for (int i = 0; i < rptTeam1.Items.Count; i++)
        {
            matchSessionresult = new MatchSessionResult(_dataObject);
            TextBox txtOver = (TextBox)rptTeam1.Items[i].FindControl("txtOver");
            TextBox txtRuns = (TextBox)rptTeam1.Items[i].FindControl("txtRuns");

            if (!string.IsNullOrEmpty(txtOver.Text) && !string.IsNullOrEmpty(txtRuns.Text))
            {
                matchSessionresult.MatchScheduleId = Convert.ToInt32(drpMatch.SelectedValue);
                matchSessionresult.TeamId = Convert.ToInt32(hdnTeam1Id.Value);
                matchSessionresult.Over = Over = Convert.ToInt32(txtOver.Text);
                matchSessionresult.Runs = Convert.ToInt32(txtRuns.Text);
                matchSessionresult.CreatedDate = System.DateTime.UtcNow;
                matchSessionresult.CreatedBy = Convert.ToInt32(cookie["AdminId"]);
                matchSessionresult.DeleteAndCreate();


                #region Call SP for calculations
                int matchTypeId = 0;
                result = new Result(_dataObject);
                matchSchedule = new MatchSchedule(_dataObject);
                List<GetMatchDetailsResult> lst = new List<GetMatchDetailsResult>();
                lst = matchSchedule.GetMatchDetails(Convert.ToInt32(drpMatch.SelectedValue));
                if (lst.Count > 0)
                {
                    matchTypeId = Convert.ToInt32(lst[0].MatchTypeId);
                }
                result.CalculateWinningResultsForCustomers(Convert.ToInt32(drpMatch.SelectedValue), 1, matchTypeId, Over, Convert.ToInt32(matchSessionresult.TeamId));
                result.CalculateNineEleven(Convert.ToInt32(drpMatch.SelectedValue), 5, matchTypeId, Over, Convert.ToInt32(matchSessionresult.TeamId));
                #endregion
            }
        }
        #endregion

        #region Team2
        for (int j = 0; j < rptTeam2.Items.Count; j++)
        {
            matchSessionresult = new MatchSessionResult(_dataObject);
            TextBox txtOver = (TextBox)rptTeam2.Items[j].FindControl("txtOver1");
            TextBox txtRuns = (TextBox)rptTeam2.Items[j].FindControl("txtRuns1");

            if (!string.IsNullOrEmpty(txtOver.Text) && !string.IsNullOrEmpty(txtRuns.Text))
            {
                matchSessionresult.MatchScheduleId = Convert.ToInt32(drpMatch.SelectedValue);
                matchSessionresult.TeamId = Convert.ToInt32(hdnTeam2Id.Value);
                matchSessionresult.Over = Over = Convert.ToInt32(txtOver.Text);
                matchSessionresult.Runs = Convert.ToInt32(txtRuns.Text);
                matchSessionresult.CreatedDate = System.DateTime.UtcNow;
                matchSessionresult.CreatedBy = Convert.ToInt32(cookie["AdminId"]);
                matchSessionresult.DeleteAndCreate();


                #region Call SP for calculations
                int matchTypeId = 0;
                result = new Result(_dataObject);
                matchSchedule = new MatchSchedule(_dataObject);
                List<GetMatchDetailsResult> lst = new List<GetMatchDetailsResult>();
                lst = matchSchedule.GetMatchDetails(Convert.ToInt32(drpMatch.SelectedValue));
                if (lst.Count > 0)
                {
                    matchTypeId = Convert.ToInt32(lst[0].MatchTypeId);
                }
                result.CalculateWinningResultsForCustomers(Convert.ToInt32(drpMatch.SelectedValue), 1, matchTypeId, Over, Convert.ToInt32(matchSessionresult.TeamId));
                result.CalculateNineEleven(Convert.ToInt32(drpMatch.SelectedValue), 5, matchTypeId, Over, Convert.ToInt32(matchSessionresult.TeamId));
                #endregion
            }
        }

        layerSuccess.InnerHtml = "<strong>Success!</strong> Record added successfully.";
        layerSuccess.Attributes.Add("style", "display:block;");
        layerError.Attributes.Add("style", "display:none;");
        #endregion

        //#region Call SP for calculations
        //int matchTypeId = 0;
        //result = new Result(_dataObject);
        //matchSchedule = new MatchSchedule(_dataObject);
        //List<GetMatchDetailsResult> lst = new List<GetMatchDetailsResult>();
        //lst = matchSchedule.GetMatchDetails(Convert.ToInt32(drpMatch.SelectedValue));
        //if (lst.Count > 0)
        //{
        //     matchTypeId = Convert.ToInt32(lst[0].MatchTypeId);
        //}
        //result.CalculateWinningResultsForCustomers(Convert.ToInt32(drpMatch.SelectedValue), 1, matchTypeId, Over,Convert.ToInt32(matchSessionresult.TeamId));
        //#endregion

        bindOvers();
        Response.Redirect("SessionResult.aspx");
    }
}